<template>
  <div class="sidebar">
    <div class="logo-container">
      <div class="logo-wrapper">
        <img src="@/assets/logo.svg" class="logo" :class="{ 'no-margin': isCollapse }" alt="logo">
        <h1 v-show="!isCollapse">苍穹外卖</h1>
      </div>
    </div>
    <el-scrollbar>
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        :collapse-width="96"
        unique-opened
        router
        @select="handleSelect"
      >
        <el-menu-item index="/dashboard">
          <el-icon><HomeFilled /></el-icon>
          <template #title>工作台</template>
        </el-menu-item>
        
        <el-menu-item index="/statistics">
          <el-icon><Histogram /></el-icon>
          <template #title>数据统计</template>
        </el-menu-item>

        <el-menu-item index="/orders">
          <el-icon><Document /></el-icon>
          <template #title>订单管理</template>
        </el-menu-item>

        <el-menu-item index="/setmeal">
          <el-icon><Menu /></el-icon>
          <template #title>套餐管理</template>
        </el-menu-item>

        <el-menu-item index="/dish">
          <el-icon><Food /></el-icon>
          <template #title>菜品管理</template>
        </el-menu-item>

        <el-menu-item index="/category">
          <el-icon><Notebook /></el-icon>
          <template #title>分类管理</template>
        </el-menu-item>

        <el-menu-item index="/employee" v-if="isAdmin">
          <el-icon><UserFilled /></el-icon>
          <template #title>员工管理</template>
        </el-menu-item>
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script setup>
import { computed, ref, watch, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAppStore } from '@/stores/app'
import { 
  HomeFilled, 
  Histogram,
  Document, 
  Menu, 
  Food, 
  Notebook,
  UserFilled
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const route = useRoute()
const router = useRouter()
const appStore = useAppStore()

const isCollapse = computed(() => appStore.sidebarCollapsed)
const activeMenu = computed(() => route.path)

// 根据收缩状态设置不同的Logo容器高度
const logoHeight = computed(() => isCollapse.value ? '90px' : '70px')

// 判断当前用户是否为admin账号
const isAdmin = computed(() => {
  const userInfo = appStore.userInfo
  return userInfo?.username === 'admin' || userInfo?.id === 1
})

// 处理菜单项选择const handleSelect = (key) => {  console.log('点击菜单项:', key)    if (key === '/orders') {    console.log('准备导航到订单管理页面')    // 使用编程式导航确保跳转成功    router.push('/')      .then(() => {        console.log('先导航到根路径')        setTimeout(() => {          router.push('/orders')            .then(() => {              console.log('导航成功，当前路径:', router.currentRoute.value.path)              ElMessage.success('进入订单管理页面')            })            .catch(err => {              console.error('导航失败:', err)              ElMessage.error('导航失败:' + err.message)            })        }, 100)      })      .catch(err => {        console.error('初始导航失败:', err)      })  }}

// 挂载时检查当前路由
onMounted(() => {
  console.log('侧边栏组件挂载，当前路由:', route.path)
})
</script>

<style scoped>
.sidebar {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.logo-container {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  background: #263445;
  border-bottom: 1px solid #1f2d3d;
  transition: height 0.3s;
  height: v-bind('logoHeight');
}

.logo-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.logo {
  height: v-bind('isCollapse ? "60px" : "50px"');
  width: v-bind('isCollapse ? "60px" : "50px"');
  margin-right: v-bind('isCollapse ? "0" : "12px"');
  transition: all 0.3s;
  border-radius: 50%;
  box-shadow: 0 0 8px rgba(0, 0, 0, 0.2);
}

.logo.no-margin {
  margin-right: 0;
  height: 70px;
  width: 70px;
  padding: 4px;
  border-radius: 50%;
  box-shadow: 0 0 12px rgba(24, 144, 255, 0.3);
}

h1 {
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  margin: 0;
  white-space: nowrap;
}

.el-menu {
  border-right: none;
}

:deep(.el-menu--collapse) {
  width: 96px;
}

:deep(.el-menu-item) {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 56px;
  line-height: 56px;
  position: relative;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.5, 1);
  overflow: hidden;
}

:deep(.el-menu:not(.el-menu--collapse) .el-menu-item) {
  justify-content: flex-start;
  padding-left: 20px !important;
}

:deep(.el-menu:not(.el-menu--collapse) .el-menu-item .el-icon) {
  margin-right: 10px;
}

:deep(.el-menu--collapse .el-menu-item) {
  padding: 0 !important;
  height: 56px;
  justify-content: center;
}

:deep(.el-menu--collapse .el-menu-item .el-icon) {
  margin: 0 auto;
  font-size: 24px;
}

:deep(.el-menu-item):before {
  content: '';
  position: absolute;
  left: 0;
  bottom: 0;
  width: 0;
  height: 2px;
  background-color: var(--el-color-primary);
  transition: width 0.3s;
}

:deep(.el-menu-item):hover:before,
:deep(.el-menu-item.is-active):before {
  width: 100%;
}

:deep(.el-menu-item.is-active) {
  background-color: #263445 !important;
  transform: translateY(-2px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

:deep(.el-menu-item):hover {
  background-color: #263445 !important;
  transform: translateY(-1px);
}

:deep(.el-menu-item:active) {
  transform: translateY(0);
  transition: transform 0.1s;
}

:deep(.el-menu-item .el-icon) {
  transition: all 0.3s;
}

:deep(.el-menu-item:hover .el-icon) {
  transform: scale(1.2);
  color: var(--el-color-primary);
}

:deep(.el-menu-item.is-active .el-icon) {
  transform: scale(1.1);
}

:deep(.el-sub-menu__title:hover) {
  background-color: #263445 !important;
}
</style> 